<script type="text/javascript">
	$(function() {
		var pg = App.wechat.wechatEdit.ui , pp = pg.params;
		var frm = {
			code : pg.seek('code'),
			name : pg.seek('name'),
			templateId : pg.seek('templateId'),
			platformFakeId : pg.seek('platformFakeId'),
			status : pg.seek('status'),
			miniProgramAppId : pg.seek('miniProgramAppId'),
			miniProgramPagePath : pg.seek('miniProgramPagePath'),
			url : pg.seek('url'),
			sendAccKey : pg.seek('sendAccKey'),
			dataKey : pg.seek('dataKey'),
			dataColor : pg.seek('dataColor'),
			dataRule : pg.seek('dataRule'),
			content : pg.seek('content'),
			device : pg.seek('device'),
			memo : pg.seek('memo')
		};
		var setValues = function (ps) {
			if (ps) {
				frm.code.val(ps.code || '');
				frm.name.val(ps.name || '');
				frm.templateId.val(ps.templateId || '');
				frm.platformFakeId.val(ps.platformFakeId || '');
				frm.status.val(ps.status || '');
				frm.miniProgramAppId.val(ps.miniProgramAppId || '');
				frm.miniProgramPagePath.val(ps.miniProgramPagePath || '');
				frm.url.val(ps.url || '');
				frm.sendAccKey.val(ps.sendAccKey || '');
				frm.dataKey.val(ps.dataKey || '');
				frm.dataColor.val(ps.dataColor || '');
				frm.dataRule.val(ps.dataRule || '');
				frm.content.val(ps.content || '');
				frm.device.val(ps.device || '');
				frm.memo.val(ps.memo || '');
			}
		};
		var getValues = function () {
			return {
				code : frm.code.val(),
				name : frm.name.val(),
				templateId : frm.templateId.val(),
				platformFakeId : frm.platformFakeId.val(),
				status : frm.status.val(),
				miniProgramAppId : frm.miniProgramAppId.val(),
				miniProgramPagePath : frm.miniProgramPagePath.val(),
				url : frm.url.val(),
				sendAccKey : frm.sendAccKey.val(),
				dataKey : frm.dataKey.val(),
				dataColor : frm.dataColor.val(),
				dataRule : frm.dataRule.val(),
				content : frm.content.val(),
				device : frm.device.val(),
				memo : frm.memo.val()
			};
		};
		var validate = function (ps) {
			var msg = pg.seek('msg');
			msg.html('');
			var x = App.validate, v = App.wechat.validation, rt;
			rt = x(v.wechat.code, ps.code, msg, "模板编码") 
				&& x(v.wechat.name, ps.name, msg, "模板名称")
				&& x(v.wechat.templateId, ps.templateId, msg, "模板号")
				&& x(v.wechat.platformFakeId, ps.platformFakeId, msg, "微信公众号")
				&& x(v.wechat.sendAccKey, ps.sendAccKey, msg, "发送账号ID")
				&& x(v.wechat.dataKey, ps.dataKey, msg, "关键字")
				&& x(v.wechat.content, ps.content, msg, "模板内容")
				&& x(v.wechat.dataRule, ps.dataRule, msg, "数据转化规则")
				&& x(v.wechat.device, ps.device, msg, "设备");
			if (!rt) {
				msg.fadeIn('slow');
			}
			return rt;
		};
		pg.seek('save').click(function () {
			var params = getValues();
			if (validate(params)) {
				var action = "/wechat/edit.do";
				if (pp) {
					$.extend(params, {id : pp.id || ''});
				} else {
					action = "/wechat/add.do";
				}
				
				$.post(App.ctx + action, params, function (rs) {
					var ro = mac.eval(rs);
					if (ro.success) {
						pg.callback(ro.data);
						if(pg.close) {
							pg.close();
						} else {
							console.log('else');
						}
					} else {
						var msg = pg.seek('msg');
						msg.html(mac.msg(ro.data, ro.params));
						msg.fadeIn('slow');
					}
				});
			}
		});
		pg.seek('close').click(function () {
			pg.callback(pp);
			pg.close();
		});
		
		setValues(pp);
	});
</script>
<div class="form">
	<div name="msg" class="msg"></div>
	<div class="item">
		<span class="label">模板编码&nbsp;<span class="msg">*</span></span>
		<input type="text" name="code" placeholder="模板编码">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">模板名称&nbsp;<span class="msg">*</span></span>
		<input type="text" name="name" placeholder="中文名称">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">模板号&nbsp;<span class="msg">*</span></span>
		<input type="text" name="templateId" placeholder="模板号">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">微信公众号&nbsp;<span class="msg">*</span></span>
		<input type="text" name="platformFakeId" placeholder="微信公众号">
		<div class="clear"></div>
	</div>
	<div class="item">
		<input type="hidden" name="status" placeholder="发布状态">
	</div>
	<div class="item">
		<span class="label">发送账号ID&nbsp;<span class="msg">*</span></span>
		<input type="text" name="sendAccKey" placeholder="发送账号ID">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">关键字&nbsp;<span class="msg">*</span></span>
		<input type="text" name="dataKey" placeholder="关键字">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">模板内容&nbsp;<span class="msg">*</span></span>
		<input type="text" name="content" placeholder="模板内容">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">设备&nbsp;<span class="msg">*</span></span>
		<input type="text" name="device" placeholder="设备">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">跳转小程序</span>
		<input type="text" name="miniProgramAppId" placeholder="跳转小程序">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">跳转小程序路径</span>
		<input type="text" name="miniProgramPagePath" placeholder="跳转小程序路径">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">跳转URL</span>
		<input type="text" name="url" placeholder="跳转URL">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">字体颜色</span>
		<input type="text" name="dataColor" placeholder="字体颜色">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">数据转化规则&nbsp;<span class="msg">*</span></span>
		<input type="text" name="dataRule" placeholder="数据转化规则">
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">备注</span>
		<textarea rows="3" cols="50" name="memo" style="width: 390px" placeholder="备注"></textarea>
		<div class="clear"></div>
	</div>
	<div class="bottom">
		<button name="save" type="button" class="btn radius btn-white btn-sm">保存</button>
		<button name="close" type="button" class="btn radius btn-info btn-sm">关闭</button>
	</div>
</div>
